package stud.aistar.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.core.annotation.Order;
import stud.aistar.entity.RegOffice;
import stud.aistar.vo.NumVo;
import stud.aistar.vo.OrderVo;

import java.util.List;

/**
 * @version 1.0
 * @Auther: git:jybdd - liming
 * @Description - 本类用于: 订单
 * @Date: 2019/10/4 18:27
 */

public interface OrderVoMapper {


    /**
     * 查询所有订单
     * @param id
     * @return
     */
    @Select("select reg_office.id,dept_name,sdept_name,doctor.doc_name,lpad(time(start_times),5,0) tstart," +
            "date(start_times) tdate,lpad(time(end_time),5,0) tend,reg_status,patient_id," +
            "year(reg_office.create_time) year,lpad(month(reg_office.create_time),2,0) month," +
            "lpad(day(reg_office.create_time),2,0) day,treatment.intro,worktime.numbers from reg_office " +
            "left join treatment on reg_office.id = treatment.pat_id " +
            "join department on department.id = firstdepartment_id " +
            "join second_department on second_department.id = seconddepartments_id " +
            "join doctor on doctor.id = doctor_id " +
            "join time on time.id = reg_office.time_id " +
            "join worktime on time.id = worktime.time_id and worktime.doc_id = reg_office.doctor_id " +
            "and worktime.time_id = reg_office.time_id " +
            "where (reg_office.reg_status = 0 or reg_office.reg_status = 3) and user_id = #{userId}")
    List<OrderVo> findByPid(Integer id);

    /**
     * 查询订单详情信息
     * @param id
     * @return
     */
    @Select("select reg_office.id,dept_name,sdept_name,patient_tel,doc_name,lpad(time(start_times),5,0) tstart," +
            "lpad(time(end_time),5,0) tend,reg_status,patient_id,year(create_time) year," +
            "lpad(month(create_time),2,0) month,lpad(day(create_time),2,0) day,reg_price " +
            "from reg_office " +
            "join department on department.id = firstdepartment_id " +
            "join second_department on second_department.id = seconddepartments_id " +
            "join doctor on doctor.id = doctor_id " +
            "join time on time.id = time_id " +
            "where reg_office.id = #{id}")
    OrderVo findByOId(Integer id);

    /**
     * 退号
     * @param id
     */
    @Update("update reg_office set reg_status = 2 where id = #{id}")
    void updateByOId(Integer id);

    /**
     *查询历史订单
     * @param id
     * @return
     */
    @Select("select reg_office.id,doctor_id,dept_name,sdept_name,doctor.doc_name,lpad(time(start_times),5,0) tstart," +
            "lpad(time(end_time),5,0) tend,reg_status,patient_id,year(reg_office.create_time) year," +
            "lpad(month(reg_office.create_time),2,0) month,lpad(day(reg_office.create_time),2,0) day," +
            "treatment.intro " +
            "from reg_office " +
            "left join treatment on reg_office.id = treatment.pat_id  " +
            "join department on department.id = firstdepartment_id " +
            "join second_department on second_department.id = seconddepartments_id " +
            "join doctor on doctor.id = doctor_id " +
            "join time on time.id = time_id "+
            "where (reg_office.reg_status = 1 or reg_office.reg_status = 2) and user_id = #{userId}")
    List<OrderVo> findByPids(Integer id);

    @Select("select @rownum:=@rownum+1 as 'index',patient_id,create_time from (select @rownum:=0) t,reg_office group by doctor_id," +
            "time_id,patient_id,create_time,id,reg_status having time_id=#{timeId} and doctor_id=#{doctorId} and (reg_status = 0 " +
            "or reg_status = 3) " +
            "order by create_time")
    List<NumVo> findList(@Param("timeId")Integer timeId,@Param("doctorId")Integer doctorId);

}